﻿import {OnSoftPhoneEvent, OnWebSocketDisConnect} from './UniSoftPhoneEvens.js';
import { Notification } from "element-ui";

var socket_di;
var g_AgentID;
var g_socket_status = 0;
var CTICommands ={
	CmdNotDefined:0,
	CmdLogin : 1,
	CmdLogout :2,
	CmdBusy :3,
	CmdIdle :4,
	CmdMakeCall :5,
	CmdMute :6,
	CmdUnMute :7,
	CmdHold :8,
	CmdUnHold :9,
	CmdHungup :10,
	CmdTransfer :11,
	CmdSendDTMF :12,
	CmdAnswer :13,
	CmdConference :14,
	CmdConsult :15,
	CmdConsult2Conference :16,
	CmdConsult2Transfer :17,
	CmdEndConsult :18,
	CmdEndConference :19,
	CmdListen :20,
	CmdInsert :21,
	CmdIntercept :22,
	CmdWisper :23,
	CmdEndListen :24,
	CmdEndInsert :25,
	CmdEndWisper :26,
	CmdSwitchMonitor :27,
	CmdForceLogOut :28,
	CmdForceSetStatus :29,
	CmdSetCtiData :30,
	CmdGetCtiData :31,
	CmdGetCurrentState :32,
	CmdSimulateCall:33,
	CmdCancelTransfer:34,
	CmdCancelConsult: 35,
	CmdCancelConfrence:36,
	CmdRest:37,
	CmdSetSysPara:38,
	CmdEnterACW:39,
	CmdLeaveACW:40,
	CmdGetLocalIP:41

};

var PHONE_STATE ={
	PHONE_STATE_LOGOUT : 0,				//签出状态
	PHONE_STATE_READY : 1,				//就绪状态
	PHONE_STATE_BUSY : 2,				//忙碌状态
	PHONE_STATE_WRAPUP : 3,				//话后处理
	PHONE_STATE_RING : 4,				//呼入振铃中
	PHONE_STATE_REMOTEALERTING : 5,		//呼出振铃，呼出拨号中
	PHONE_STATE_TALKING : 6,			//通话中
	PHONE_STATE_HOLD : 7,				//保持
	PHONE_STATE_CONSULT_DIALING : 8,    //咨询拨号中，即通话中点咨询到接通的状态
	PHONE_STATE_CONSULTING : 9,		//咨询中
	PHONE_STATE_CONF_DIALING : 10,		//会议拨号中，单步会议时拨号开始到会议建立的状态
	PHONE_STATE_CONFERENCEING : 11,		//会议中	三方会议
	PHONE_STATE_TRANSF_DIALING : 12,	//转接拨号中，如果是成功转，从发起转接到转接成功的状态
	PHONE_STATE_TRANSFING :13, //转接咨询中，两步转接，接通被转接对象的状态
	PHONE_STATE_BECONSULT : 14,		//被咨询中
	PHONE_STATE_BECONF : 15,		//被会议中
	PHONE_STATE_HELP_DIALING : 16, //求助拨号中
	PHONE_STATE_HELPING : 17,	//求助中，既求助时对方接听
	PHONE_STATE_LISTENING : 18, //监听中
	PHONE_STATE_INSERTING : 19, //强插中
	PHONE_STATE_WHISPER : 20, //耳语中
	PHONE_STATE_REST : 21, //离席状态
	PHONE_STATE_DIALING : 22 //外呼拨号中

}
function InitWebSocket(url){
	var nRet = 1;
	if (typeof MozWebSocket != "undefined") {
		socket_di = new MozWebSocket(url,
				   "dumb-increment-protocol");
	} else {
		//socket_di = new WebSocket(get_appropriate_ws_url(),
		socket_di = new WebSocket(url,
				   "dumb-increment-protocol");
	}
	/*if (typeof MozWebSocket != "undefined") {
		socket_di = new MozWebSocket(url);
	} else {
		//socket_di = new WebSocket(get_appropriate_ws_url(),
		socket_di = new WebSocket(url);
	}*/


	try {
		socket_di.onopen = function() {
			g_socket_status = 1;
			// document.getElementById("wsdi_statustd").style.backgroundColor = "#40ff40";
			// document.getElementById("wsdi_status").textContent = " conn opened ";
		} 

		socket_di.onmessage =function got_packet(msg) {
			//notify(msg.data);
			var obj = eval("("+msg.data+")"); 
			//notify(obj.cmd+";"+obj.cmd_description+";"+obj.parameters.AgentID+";"+obj.parameters.ErrorCode);
			OnSoftPhoneEvent(obj);
			//document.getElementById("number").textContent = msg.data + "\n";
		} 

		socket_di.onclose = function(){
			g_socket_status = 0;
			OnWebSocketDisConnect();
			
		}
	} catch(exception) {
		nRet = 0;
		notify('<p>Error' + exception);  
	}
	return nRet;
}


function SignIn(sAgentID,sAgentExt,sAgentPwd,sAgentSkill){
	var nRet = 1;
	g_AgentID = sAgentID;
	var phonejoson = {
			cmd:	CTICommands.CmdLogin,
			parameters:	{
				AgentID:	g_AgentID,
				AgentExt:	sAgentExt,
				AgentPwd:	sAgentPwd,
				AgentSkill:	sAgentSkill
			}
		};
	if(g_socket_status == 1)
	{
		try
		{
			socket_di.send(JSON.stringify(phonejoson));
		}
		catch (exception)
		{
			nRet = 0;
			notify('<p>Error' + exception);  
		}
	}else
	{
		nRet = 0;
		notify("和服务端网络链接中断，请检查网络链接");
	}
	return nRet;
}
function SignOut(){
	var nRet = 1;
	var phonejoson = {
			cmd:	CTICommands.CmdLogout,
			parameters:	{
				AgentID:	g_AgentID
			}
		};
	if(g_socket_status == 1)
	{
		try
		{
			socket_di.send(JSON.stringify(phonejoson));
		}
		catch (exception)
		{
			nRet = 0;
			notify('<p>Error' + exception);  
		}
	}else
	{
		nRet = 0;
		notify("和服务端网络链接中断，请检查网络链接");
	}
	return nRet;
}
function Busy(sReason){
	var nRet = 1;
	var phonejoson = {
			cmd:	CTICommands.CmdBusy,
			parameters:	{
				AgentID:	g_AgentID,
				Reason:		sReason
			}
		};
	if(g_socket_status == 1)
	{
		try
		{
			socket_di.send(JSON.stringify(phonejoson));
		}
		catch (exception)
		{
			nRet = 0;
			notify('<p>Error' + exception);  
		}
	}else
	{
		nRet = 0;
		notify("和服务端网络链接中断，请检查网络链接");
	}
	return nRet;
}
function Rest(sReason){
	var nRet = 1;
	var phonejoson = {
			cmd:	CTICommands.CmdRest,
			parameters:	{
				AgentID:	g_AgentID,
				Reason:		sReason
			}
		};
	if(g_socket_status == 1)
	{
		try
		{
			socket_di.send(JSON.stringify(phonejoson));
		}
		catch (exception)
		{
			nRet = 0;
			notify('<p>Error' + exception);  
		}
	}else
	{
		nRet = 0;
		notify("和服务端网络链接中断，请检查网络链接");
	}
	return nRet;
}
function Idle(){
	var nRet = 1;
	var phonejoson = {
			cmd:	CTICommands.CmdIdle,
			parameters:	{
				AgentID:	g_AgentID
			}
		};
	if(g_socket_status == 1)
	{
		try
		{
			socket_di.send(JSON.stringify(phonejoson));
		}
		catch (exception)
		{
			nRet = 0;
			notify('<p>Error' + exception);  
		}
	}else
	{
		nRet = 0;
		notify("和服务端网络链接中断，请检查网络链接");
	}
	return nRet;
}
function Answer(){
	var nRet = 1;
	var phonejoson = {
			cmd:	CTICommands.CmdAnswer,
			parameters:	{
				AgentID:	g_AgentID
			}
		};
	if(g_socket_status == 1)
	{
		try
		{
			socket_di.send(JSON.stringify(phonejoson));
		}
		catch (exception)
		{
			nRet = 0;
			notify('<p>Error' + exception);  
		}
	}else
	{
		nRet = 0;
		notify("和服务端网络链接中断，请检查网络链接");
	}
	return nRet;
}
function Hungup(){
	var nRet = 1;
	var phonejoson = {
			cmd:	CTICommands.CmdHungup,
			parameters:	{
				AgentID:	g_AgentID
			}
		};
	if(g_socket_status == 1)
	{
		try
		{
			socket_di.send(JSON.stringify(phonejoson));
		}
		catch (exception)
		{
			nRet = 0;
			notify('<p>Error' + exception);  
		}
	}else
	{
		nRet = 0;
		notify("和服务端网络链接中断，请检查网络链接");
	}
	return nRet;
}
function Hold(){
	var nRet = 1;
	var phonejoson = {
			cmd:	CTICommands.CmdHold,
			parameters:	{
				AgentID:	g_AgentID
			}
		};
	if(g_socket_status == 1)
	{
		try
		{
			socket_di.send(JSON.stringify(phonejoson));
		}
		catch (exception)
		{
			nRet = 0;
			notify('<p>Error' + exception);  
		}
	}else
	{
		nRet = 0;
		notify("和服务端网络链接中断，请检查网络链接");
	}
	return nRet;
}
function UnHold(){
	var nRet = 1;
	var phonejoson = {
			cmd:	CTICommands.CmdUnHold,
			parameters:	{
				AgentID:	g_AgentID
			}
		};
	if(g_socket_status == 1)
	{
		try
		{
			socket_di.send(JSON.stringify(phonejoson));
		}
		catch (exception)
		{
			nRet = 0;
			notify('<p>Error' + exception);  
		}
	}else
	{
		nRet = 0;
		notify("和服务端网络链接中断，请检查网络链接");
	}
	return nRet;
}


function MakeCall(sUserNumber,sNumberType){
	var nRet = 1;
	var phonejoson = {
			cmd:	CTICommands.CmdMakeCall,
			parameters:	{
				AgentID:	g_AgentID,
				UserNumber:	sUserNumber,
				NumType:	sNumberType
			}
		};
	if(g_socket_status == 1)
	{
		try
		{
			socket_di.send(JSON.stringify(phonejoson));
		}
		catch (exception)
		{
			nRet = 0;
			notify('<p>Error' + exception);  
		}
	}else
	{
		nRet = 0;
		notify("和服务端网络链接中断，请检查网络链接");
	}
	return nRet;
}
function Consult(sUserNumber,sNumberType,sConsultType){
	var nRet = 1;
	var phonejoson = {
			cmd:	CTICommands.CmdConsult,
			parameters:	{
				AgentID:		g_AgentID,
				UserNumber:		sUserNumber,
				NumType:		sNumberType,
				ConsultType:	sConsultType
			}
		};
	if(g_socket_status == 1)
	{
		try
		{
			socket_di.send(JSON.stringify(phonejoson));
		}
		catch (exception)
		{
			nRet = 0;
			notify('<p>Error' + exception);  
		}
	}else
	{
		nRet = 0;
		notify("和服务端网络链接中断，请检查网络链接");
	}
	return nRet;
}
function Conference(sUserNumber,sNumberType){
	var nRet = 1;
	var phonejoson = {
			cmd:	CTICommands.CmdConference,
			parameters:	{
				AgentID:	g_AgentID,
				UserNumber:	sUserNumber,
				NumType:	sNumberType
			}
		};
	if(g_socket_status == 1)
	{
		try
		{
			socket_di.send(JSON.stringify(phonejoson));
		}
		catch (exception)
		{
			nRet = 0;
			notify('<p>Error' + exception);  
		}
	}else
	{
		nRet = 0;
		notify("和服务端网络链接中断，请检查网络链接");
	}
	return nRet;
}
function Transfer(sUserNumber,sNumberType,sTransfType){
	var nRet = 1;
	var phonejoson = {
			cmd:	CTICommands.CmdTransfer,
			parameters:	{
				AgentID:	g_AgentID,
				UserNumber:	sUserNumber,
				NumType:	sNumberType,
				TransfType:	sTransfType
			}
		};
	if(g_socket_status == 1)
	{
		try
		{
			socket_di.send(JSON.stringify(phonejoson));
		}
		catch (exception)
		{
			nRet = 0;
			notify('<p>Error' + exception);  
		}
	}else
	{
		nRet = 0;
		notify("和服务端网络链接中断，请检查网络链接");
	}
	return nRet;
}
function Consult2Conference(){
	var nRet = 1;
	var phonejoson = {
			cmd:	CTICommands.CmdConsult2Conference,
			parameters:	{
				AgentID:	g_AgentID
			}
		};
	if(g_socket_status == 1)
	{
		try
		{
			socket_di.send(JSON.stringify(phonejoson));
		}
		catch (exception)
		{
			nRet = 0;
			notify('<p>Error' + exception);  
		}
	}else
	{
		nRet = 0;
		notify("和服务端网络链接中断，请检查网络链接");
	}
	return nRet;
}
function Consult2Transfer(){
	var nRet = 1;
	var phonejoson = {
			cmd:	CTICommands.CmdConsult2Transfer,
			parameters:	{
				AgentID:	g_AgentID
			}
		};
	if(g_socket_status == 1)
	{
		try
		{
			socket_di.send(JSON.stringify(phonejoson));
		}
		catch (exception)
		{
			nRet = 0;
			notify('<p>Error' + exception);  
		}
	}else
	{
		nRet = 0;
		notify("和服务端网络链接中断，请检查网络链接");
	}
	return nRet;
}

function SendDTMF(sDTMF){
	var nRet = 1;
	var phonejoson = {
			cmd:	CTICommands.CmdSendDTMF,
			parameters:	{
				AgentID:	g_AgentID,
				DTMF:		sDTMF
			}
		};
	if(g_socket_status == 1)
	{
		try
		{
			socket_di.send(JSON.stringify(phonejoson));
		}
		catch (exception)
		{
			nRet = 0;
			notify('<p>Error' + exception);  
		}
	}else
	{
		nRet = 0;
		notify("和服务端网络链接中断，请检查网络链接");
	}
	return nRet;
}
function SetCtiData(sKey,sValue){
	var nRet = 1;
	var phonejoson = {
			cmd:	CTICommands.CmdSetCtiData,
			parameters:	{
				AgentID:	g_AgentID,
				Key:		sKey,
				Value:		sValue
			}
		};
	if(g_socket_status == 1)
	{
		try
		{
			socket_di.send(JSON.stringify(phonejoson));
		}
		catch (exception)
		{
			nRet = 0;
			notify('<p>Error' + exception);  
		}
	}else
	{
		nRet = 0;
		notify("和服务端网络链接中断，请检查网络链接");
	}
	return nRet;
}
function SetSysPara(sKey,sValue){
	var nRet = 1;
	var phonejoson = {
			cmd:	CTICommands.CmdSetSysPara,
			parameters:	{
				AgentID:	g_AgentID,
				Key:		sKey,
				Value:		sValue
			}
		};
	if(g_socket_status == 1)
	{
		try
		{
			socket_di.send(JSON.stringify(phonejoson));
		}
		catch (exception)
		{
			nRet = 0;
			notify('<p>Error' + exception);  
		}
	}else
	{
		nRet = 0;
		notify("和服务端网络链接中断，请检查网络链接");
	}
	return nRet;
}

/*function OnCtiDataReturn(jsonp){  
	notify("message:"+jsonp);  
}*/

function notify(msg) {
    // if(process.env.NODE_ENV === 'production') return;
    Notification({
		title: '提示',
        type: 'warning',
        duration: 1500,
		message: msg
	})
}

export {
    InitWebSocket,
    GetLocalIP,
    SimulateCall,
    SignIn,
    SignOut,
    Busy,
    Rest,
    Idle,
    Answer,
    Hungup,
    Hold,
    UnHold,
    Mute,
    UnMute,
    MakeCall,
    Consult,
    Conference,
    Transfer,
    Consult2Conference,
    Consult2Transfer,
    CancelTransfer,
    CancelConsult,
    CancelConfrence,
    EndConference,
    EndConsult,
    Insert,
    Intercept,
    Wisper,
    Listen,
    EndInsert,
    EndListen,
    EndWisper,
    SwitchMonitor,
    ForceLogOut,
    EnterAcw,
    LeaveAcw,
    ForceSetStatus,
    SendDTMF,
    SetCtiData,
	SetSysPara,
	GetAllAgentState
}